package com.briup.estore.web.controller;

import com.alipay.api.domain.Shop;
import com.briup.estore.Service.ShopCarService;
import com.briup.estore.vo.ShopCarVO;
import com.briup.estore.web.response.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(tags = "购物车")
@RequestMapping("/shopCar")
public class ShopCarController {
    // /12          // /(bookId)  @Pathvariable
    //?bookId=12    StringbookId
    @Autowired
    private ShopCarService shopCarService;
    @PutMapping("/{bookId}")
    public Result addCar(@PathVariable int bookId ,HttpServletRequest request){
        String userIdStr = (String) request.getAttribute("userId");//"1"
        int userId = Integer.parseInt(userIdStr);// "1" -> 1
        // System.out.println("bookId = " + bookId);
        shopCarService.addCar(userId,bookId); //用户id 书籍id
        return Result.success();
    }

    @GetMapping("/")
    public Result listCar(HttpServletRequest request){
        String userIdStr = (String) request.getAttribute("userId");
        int userId = Integer.parseInt(userIdStr);
        //查询所有的购物车
        List<ShopCarVO> shopCarVOS =shopCarService.listCar(userId);
        return Result.success(shopCarVOS);
    }
}
